# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

class Login(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
		#测试URL
        self.base_url = "http://192.168.56.102:2017/zentao/user-login.html"     
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_login(self):
    	u"登录测试"
    	if False:
    		pass
    	else:
    		raise NameError('test error!!')
	
    def test_login1(self):
		u"登录测试-最短字符集"
		time.sleep(1)
		driver = self.driver
		driver.get(self.base_url + "/zentao/user-login.html")
		driver.find_element_by_id("account").clear()		
		driver.find_element_by_id("account").send_keys("123")
		driver.find_element_by_name("password").clear()
		driver.find_element_by_name("password").send_keys("123")
		driver.find_element_by_id("submit").click()
		time.sleep(1)
		alert=driver.switch_to_alert()
		if alert:
			print alert.text
			print u"测试登录失败"			
			self.assertIsNone(alert)
			alert.accept()
		else:
			print "login ok"
		# print alert.text		
		
    def test_login2(self):
		u"登录测试-特殊字符集"
		time.sleep(1)
		driver = self.driver
		driver.get(self.base_url + "/zentao/user-login.html")
		driver.find_element_by_id("account").clear()
		driver.find_element_by_id("account").send_keys("~`!@#$%^&*()_+-={}|[]\\:\";'<>?,./")
		driver.find_element_by_name("password").clear()
		driver.find_element_by_name("password").send_keys("~`!@#$%^&*()_+-={}|[]\\:\";'<>?,./")
		driver.find_element_by_id("submit").click()
		time.sleep(1)
		alert=driver.switch_to_alert()
		print u"打印弹窗信息"
		print alert.text
		self.assertEqual(alert.text,u'登录失败，请检查您的用户名或密码是否填写正确。')
		alert.accept()
		
    def test_login3(self):
		u"登录测试-错误密码"
		time.sleep(1)
		driver = self.driver
		driver.get(self.base_url + "/zentao/user-login.html")
		driver.find_element_by_id("account").clear()
		driver.find_element_by_id("account").send_keys("admin")
		driver.find_element_by_name("password").clear()
		driver.find_element_by_name("password").send_keys("123")
		driver.find_element_by_id("submit").click()
		time.sleep(1)
		alert=driver.switch_to_alert()
		alert.accept()
		
    def test_login4(self):
		u"登录测试-正确密码"
		time.sleep(1)
		driver = self.driver
		driver.get(self.base_url + "/zentao/user-login.html")
		driver.find_element_by_id("account").clear()
		driver.find_element_by_id("account").send_keys("admin")
		driver.find_element_by_name("password").clear()
		driver.find_element_by_name("password").send_keys("123456")
		driver.find_element_by_id("submit").click()
		time.sleep(2)
    
    # def is_element_present(self, how, what):
        # try: self.driver.find_element(by=how, value=what)
        # except NoSuchElementException as e: return False
        # return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException as e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

#构建测试集
def suite():
	suite=unittest.TestSuite()
	# suite.addTest(Login("test_login"))
	suite.addTest(unittest.makeSuite(Login, "test"))
	return suite

if __name__ == "__main__":
    unittest.main(defaultTest='suite')
	# unittest.main()